package com.codeplay.service.impl;

import com.codeplay.entity.ProjectComment;
import com.codeplay.entity.ProjectComment.UserRole;
import com.codeplay.repository.ProjectCommentRepository;
import com.codeplay.service.ProjectCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ProjectCommentServiceImpl implements ProjectCommentService {

    @Autowired
    private ProjectCommentRepository projectCommentRepository;

    @Override
    public ProjectComment createComment(ProjectComment comment) {
        comment.setCreatedAt(LocalDateTime.now());
        comment.setUpdatedAt(LocalDateTime.now());
        comment.setIsReviewed(false);
        comment.setIsAppropriate(true); // 默认适合，等待审核
        return projectCommentRepository.save(comment);
    }

    @Override
    public ProjectComment updateComment(Long id, String content) {
        ProjectComment comment = projectCommentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Comment not found with id: " + id));
        comment.setContent(content);
        comment.setUpdatedAt(LocalDateTime.now());
        comment.setIsReviewed(false); // 更新后需要重新审核
        return projectCommentRepository.save(comment);
    }

    @Override
    public void deleteComment(Long id) {
        projectCommentRepository.deleteById(id);
    }

    @Override
    public ProjectComment getComment(Long id) {
        return projectCommentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Comment not found with id: " + id));
    }

    @Override
    public List<ProjectComment> getCommentsByProject(Long projectId) {
        return projectCommentRepository.findByProjectId(projectId);
    }

    @Override
    public List<ProjectComment> getCommentsByTask(Long taskId) {
        return projectCommentRepository.findByTaskId(taskId);
    }

    @Override
    public List<ProjectComment> getCommentsByUser(Long userId) {
        return projectCommentRepository.findByUserId(userId);
    }

    @Override
    public List<ProjectComment> getCommentsByProjectAndTask(Long projectId, Long taskId) {
        return projectCommentRepository.findByProjectIdAndTaskId(projectId, taskId);
    }

    @Override
    public List<ProjectComment> getPendingReviewComments() {
        return projectCommentRepository.findByIsReviewedFalse();
    }

    @Override
    public ProjectComment reviewComment(Long id, Long reviewerId, boolean isAppropriate) {
        ProjectComment comment = getComment(id);
        comment.setIsReviewed(true);
        comment.setIsAppropriate(isAppropriate);
        comment.setReviewedBy(reviewerId);
        comment.setReviewTime(LocalDateTime.now());
        return projectCommentRepository.save(comment);
    }

    @Override
    public List<ProjectComment> getCommentsByReviewer(Long reviewerId) {
        return projectCommentRepository.findByReviewedBy(reviewerId);
    }

    @Override
    public List<ProjectComment> getInappropriateComments() {
        return projectCommentRepository.findByIsAppropriate(false);
    }

    @Override
    public List<ProjectComment> getChildComments(Long childUserId) {
        return projectCommentRepository.findByUserIdAndUserRole(childUserId, UserRole.CHILD);
    }

    @Override
    public List<ProjectComment> getParentVisibleComments(Long projectId) {
        return projectCommentRepository.findByProjectIdAndParentVisibleTrue(projectId);
    }

    @Override
    public void setParentVisibility(Long commentId, boolean visible) {
        ProjectComment comment = getComment(commentId);
        comment.setParentVisible(visible);
        projectCommentRepository.save(comment);
    }

    @Override
    public List<ProjectComment> getCommentsByUserRole(Long userId, UserRole role) {
        return projectCommentRepository.findByUserIdAndUserRole(userId, role);
    }

    @Override
    public List<ProjectComment> getSafeCommentsByProject(Long projectId) {
        return projectCommentRepository.findByProjectIdAndIsAppropriateTrue(projectId);
    }

    @Override
    public List<ProjectComment> getSafeCommentsByTask(Long taskId) {
        return projectCommentRepository.findByTaskIdAndIsAppropriateTrue(taskId);
    }

    @Override
    public List<ProjectComment> getSafeCommentsByUser(Long userId) {
        return projectCommentRepository.findByUserIdAndIsAppropriateTrue(userId);
    }

    @Override
    public void deleteCommentsByUserAndRole(Long userId, UserRole role) {
        projectCommentRepository.deleteByUserIdAndUserRole(userId, role);
    }

    @Override
    public void deleteCommentsByProject(Long projectId) {
        projectCommentRepository.deleteByProjectId(projectId);
    }

    @Override
    public void deleteCommentsByTask(Long taskId) {
        projectCommentRepository.deleteByTaskId(taskId);
    }
} 